package cn.zhangjie.xhr;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class Producer {


    public static void main(String[] args) throws Exception {
        //1.创建生产者对象，指定生产者组名
        DefaultMQProducer Producer = new DefaultMQProducer("xhr-producer-group");
        //2.连接mq
        Producer.setNamesrvAddr("127.0.0.1:9876");
        //3.启动
        Producer.start();
        //4.创建消息，设置消息对象
        Message message = new Message("xhr", "hello", "我要回家".getBytes(StandardCharsets.UTF_8));
        Producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println(sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                throwable.printStackTrace();
                System.out.println("发送消息失败");
            }
        });
    }
}
